/**
 * 
 */
package com.smarthome.core.housemanager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShBuidingTypeService;
import com.smarthome.core.housemanager.BuildingTypeService;
import com.smarthome.domain.bean.ShBuidingType;
import com.smarthome.domain.bean.ShBuilding;
import com.smarthome.domain.bean.ShDoor;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * @author dhc
 * @date 2016年3月7日 下午11:54:12
 * @title
 */
@Service("buildingTypeService")
public class BuildingTypeServiceImpl implements BuildingTypeService {

	@Resource(name = "shBuidingTypeService")
	private ShBuidingTypeService buidingTypeService;;
	
	@Override
	public List<ShBuidingType> getList(ShBuidingType buidingType) {
		return buidingTypeService.selectEntryList(buidingType);
	}

	@Override
	public int add(ShBuidingType type) {
		return buidingTypeService.saveOrUpdate(type);
	}

	@Override
	public int edit(ShBuidingType type) {
		ShBuidingType saveBuilding = buidingTypeService.selectEntryById(type.getId());
		if(saveBuilding != null){
			BeanUtils.copyProperties(type, saveBuilding);
			return buidingTypeService.saveOrUpdate(saveBuilding);
		}
		
		return 0;
	}

	@Override
	public int delte(Long id) {
		return buidingTypeService.deleteByIds(id);
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.BuildingTypeService#getDataGrid(com.smarthome.domain.bean.ShBuidingType, com.smarthome.domain.vo.boss.PageHelper)
	 */
	@Override
	public DataGrid getDataGrid(ShBuidingType buidingType, PageHelper page) {
		
		return buidingTypeService.getDataGrid(buidingType, page);
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.BuildingTypeService#getBuidingTypeById(java.lang.Long)
	 */
	@Override
	public ShBuidingType getBuidingTypeById(Long id) {
		return buidingTypeService.selectEntryById(id);
	}

	/* (non-Javadoc)
	 * @see com.smarthome.core.housemanager.BuildingTypeService#isRepeat(com.smarthome.domain.bean.ShBuidingType)
	 */
	@Override
	public boolean isRepeat(ShBuidingType buidingType) {
		if(null == buidingType) return false;
		ShBuidingType query = new ShBuidingType();
		query.setName(buidingType.getName());
//		query.setId(buidingType.getId());
		query.setResidenceId(buidingType.getResidenceId());
		List<ShBuidingType> types = buidingTypeService.selectEntryList(query);
		if(types == null || types.size() == 0){			
			return false;
		}
		
		for(ShBuidingType type : types){
			if(!type.getId().equals(buidingType.getId())) return true;
		}
		return false;
	}

}
